package game
{
	import assets.Asset;
	
	import controller.TestActionController;
	
	import events.GameEvent;
	import events.LoadedEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.Responder;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	import gameObject.RobotPart;
	import gameObject.WeaponPartMain;
	import gameObject.WeaponPartSec;
	import gameObject.resources3D.RobotResource;
	import gameObject.resources3D.WeaponConnector;
	import gameObject.resources3D.WeaponResource;
	
	import tool.BitmapTool;
	import tool.tweener.Tweener;
	
	import ui.component.Button;
	import ui.component.ConfirmBox;
	import ui.component.MultScrollBar;
	import ui.component.ProgressBar;
	import ui.component.RobotInfo;
	
	public class StoreHouse extends Sprite
	{
		private var _cameraSetting:String = Global.iniAddress+"SH_Camera.xml";
		private var _lightsSetting:String = Global.iniAddress+"SH_Lights.xml";
		
		private var _xmlURL:String = Global.assetPath+"UI/jjzb_scbar.xml";
		private var _xmlLoader:URLLoader;
		private var _bmdLoader:Loader;
		private var _xml:XML;
		private var _bitmapURL:String;
		
		private var _bitmapDatas:Dictionary;
		/////////////////////////////////////
		
		private var _robotDatas:Array = new Array;
		private var _mainWeaponDatas:Array = new Array;
		private var _secWeaponDatas:Array = new Array;
		
		private var _robotParts:Vector.<RobotPart> = new Vector.<RobotPart>;
		private var _mainWeaponParts:Vector.<WeaponPartMain> = new Vector.<WeaponPartMain>;
		private var _secWeaponParts:Vector.<WeaponPartSec> = new Vector.<WeaponPartSec>;
		
		////////////////////////////////////////////////////
		private var _title:Bitmap;
		private var _progressBar:ProgressBar;
		private var _assetsTotle:uint = 0;
		private var _assetsCurrent:uint = 0;
		
		private var _mainScrollBar:MultScrollBar;
		private var _confirmBox:ConfirmBox;
		private var _quitButton:Button;
		private var _equipRobotID:int;
		////////////////////////////////////////////////////记录上传给服务器的操作指令
		private var _robSel:Array = new Array;	//卖机体的指令
		private var _mainSel:Array = new Array;	//卖主武器的指令
		private var _secSel:Array = new Array;	//卖副武器的指令
		
		////////////////////////////////////////////////////parameters
		private var _paramNames:Array = [
			["装备等级:","防御力:","移动速度:","HP:","耐久值:"],
			["装备等级:","子弹量:","射击速度:","攻击力:","耐久值:"],
			["装备等级:","射击速度:","攻击力:","耐久值:",""],
			["","","","",""]
		];
		
		private var _paramTypes:Array = [
			[0,0,0,1,1],
			[0,0,0,0,1],
			[0,0,0,1,2],
			[2,2,2,2,2]
		];
		////////////////////////////////////////////////////
		public function StoreHouse()
		{
			addChild(Global.user);
			
			_confirmBox = new ConfirmBox;//初始化确认框
			
			_progressBar = new ProgressBar(400);//进度条
			addChild(_progressBar);
			
			var settingLoader:URLLoader = new URLLoader;//取灯光相机
			settingLoader.addEventListener(Event.COMPLETE,cameraLoaded);
			settingLoader.load(new URLRequest(_cameraSetting));
			
			_xmlLoader = new URLLoader;//取ui美术资源
			_xmlLoader.addEventListener(Event.COMPLETE,xmlLoaded);
			_xmlLoader.load(new URLRequest(_xmlURL));
		}
		
		public function dispose():void
		{
			for each(var robPart:RobotPart in _robotParts)robPart.dispose();
		}
		
		////////////////////////////////////////////////////
		private function xmlLoaded(e:Event):void
		{
			_xmlLoader.removeEventListener(Event.COMPLETE,xmlLoaded);
			_xml = new XML(_xmlLoader.data);
			
			_bitmapURL = Global.assetPath+_xml.bitmaps.@ima;
			
			Asset.bitmapdata.addEventListener(LoadedEvent.BITMAPDATA_COMPLETE,bmdLoaded);
			Asset.bitmapdata.load(_bitmapURL);
		}
		
		private function bmdLoaded(e:LoadedEvent):void
		{
			if(e.URL==_bitmapURL)
			{
				Asset.bitmapdata.removeEventListener(LoadedEvent.BITMAPDATA_COMPLETE,bmdLoaded);
				var bmd:BitmapData = Asset.resBitmapDatas[_bitmapURL];
				_bitmapDatas = BitmapTool.cutImageUseXML(bmd,new XML(_xml.bitmaps));
				var _quitBmds:Array = BitmapTool.cutImageWithSameSize(bmd,3,1,0,227,96,30);
				
				_mainScrollBar = new MultScrollBar(_bitmapDatas,bmd,_xml);
				_quitButton = new Button(_quitBmds[0],_quitBmds[1],_quitBmds[2],onQuit);
				
				Global.phpServer.call("robotServer.getRobots", new Responder(gotRobots),Global.uid);//获取玩家拥有的机甲
			}
		}
		
		private function onQuit():void
		{
			///////////退出时向服务端更新机甲装备和武器的安装情况
			var equipID:uint = _equipRobotID;
			var wepData_m:Array = new Array;
			var wepData_s:Array = new Array;
			
			for each(var rob:RobotPart in _robotParts)
			{
				var wep_m:Array = new Array;
				for each(var pid:int in rob.wep_m)wep_m.push(pid);
				var wep_s:Array = new Array;
				for each(pid in rob.wep_s)wep_s.push(pid);
				
				wepData_m.push(wep_m);
				wepData_s.push(wep_s);
			}
			Global.phpServer.call("robotServer.updateRobots", new Responder(updated),Global.uid,equipID,wepData_m,wepData_s);
			
			/////////清理视图
			parent.removeChild(this);
			
			Global.gameView.scene.removeChild(_robotParts[_equipRobotID]);
			
			Global.gc();
			Global.stage.addChild(new Citadel);
		}
		
		/////////////////////////////////////////////////////
		private function gotRobots(obj:Object):void//收到机甲数据
		{
			_progressBar.value = 40;
			
			_robotDatas = obj as Array;
			_assetsTotle+=_robotDatas.length;
			Global.phpServer.call("robotServer.getMainWeapons", new Responder(gotMainWeapons),Global.uid);//获取玩家拥有的主武器
		}
		
		private function gotMainWeapons(obj:Object):void//收到主武器数据
		{
			_progressBar.value = 70;
			
			_mainWeaponDatas = obj as Array;
			if(_mainWeaponDatas)_assetsTotle+=_mainWeaponDatas.length;
			
			Global.phpServer.call("robotServer.getSecWeapons", new Responder(gotSecWeapons),Global.uid);//获取玩家拥有的副武器
		}
		
		private function gotSecWeapons(obj:Object):void//收到副武器数据
		{
			_progressBar.value = 100;
			
			_secWeaponDatas = obj as Array;
			if(_secWeaponDatas)_assetsTotle+=_secWeaponDatas.length;
			
			
			loadAssets();//资源列表齐全后开始下载
		}
		
		private function loadAssets():void//下载玩家仓库中的所有资源
		{
			for each(var val:Object in _robotDatas)
			{
				var robPart:RobotPart = new RobotPart(val.id,val.inc);
				robPart.addEventListener(Event.COMPLETE,gotAsset);
				
				robPart.equip = val.equip==1?true:false;
				robPart.hp = val.hp;
				robPart.dur = val.dur;
				
				Global.gameView.scene.addChild(robPart);
				
				var wep_Str:String = val.wep_m;
				var Str_ary:Array = wep_Str.split(",");
				for each(var pid:String in Str_ary)robPart.wep_m.push(int(pid));
				
				wep_Str = val.wep_s;
				Str_ary = wep_Str.split(",");
				for each(pid in Str_ary)robPart.wep_s.push(int(pid));
				
				_robotParts.push(robPart);
				
				if(robPart.equip)_equipRobotID = _robotParts.length-1;
				
				_mainScrollBar.addItem(1,robPart.icon);
			}
			
			for each(val in _mainWeaponDatas)
			{
				var mainWep:WeaponPartMain = new WeaponPartMain(val.id,val.inc);
				mainWep.addEventListener(Event.COMPLETE,gotAsset);
				
				mainWep.pid = val.pid;
				mainWep.dur = val.dur;
				_mainWeaponParts.push(mainWep);
				
				_mainScrollBar.addItem(2,mainWep.icon);
			}
			
			for each(val in _secWeaponDatas)
			{
				var secWep:WeaponPartSec = new WeaponPartSec(val.id,val.inc);
				secWep.addEventListener(Event.COMPLETE,gotAsset);
				
				secWep.pid = val.pid;
				secWep.dur = val.dur;
				_secWeaponParts.push(secWep);
				
				_mainScrollBar.addItem(3,secWep.icon);
			}
		}
		
		private function gotAsset(e:Event):void//取得仓库中的资源
		{
			var obj:* = e.currentTarget;
			obj.removeEventListener(Event.COMPLETE,gotAsset);
			
			_assetsCurrent++;
			_progressBar.value = _assetsCurrent*100/_assetsTotle;
			
			if(_progressBar.value == 100)//当所有资源下载完后进入仓库界面
			{
				removeChild(_progressBar);
				addChild(_mainScrollBar);
				
				addChild(Global.user);
				Global.user.dispayInfo(_bitmapDatas);
				
				_title = new Bitmap(_bitmapDatas["title"]);
				_title.x = Global.stage.stageWidth-_title.bitmapData.width;
				addChild(_title);
				
				_quitButton.x = _mainScrollBar.width-_quitButton.width;
				_quitButton.y = -_quitButton.height-9;
				_mainScrollBar.addChild(_quitButton);
				Global.stage.addEventListener(Event.RESIZE,onResize);
				
				refreshMainWeapon();
				refreshSecWeapon();
				
				//侦听从MultScrollBar发来的用户输入事件
				_mainScrollBar.addEventListener(GameEvent.ITEM_CALL_SELECT,onSelectCalled);
				_mainScrollBar.addEventListener(GameEvent.ITEM_CALL_EQUIP,onEquipCalled);
				_mainScrollBar.addEventListener(GameEvent.ITEM_CALL_INSTALL,onInstallCalled);
				_mainScrollBar.addEventListener(GameEvent.ITEM_CALL_UNINSTALL,onUninstallCalled);
				_mainScrollBar.addEventListener(GameEvent.ITEM_CALL_CONNECT,onConnectCalled);
				_mainScrollBar.addEventListener(GameEvent.ITEM_CALL_INCREASE,onIncreaseCalled);
				_mainScrollBar.addEventListener(GameEvent.ITEM_CALL_SELL,onSellCalled);
				addEventListener(Event.REMOVED_FROM_STAGE,onRemoved);
				
				for each(var robPart:RobotPart in _robotParts)robPart.controller = new TestActionController;
			}
		}
		
		/////////////////////////////////////////////////////侦听到的从MultScrollBar发来的用户输入事件
		private function onSelectCalled(e:GameEvent):void//更新参数面板上的数据
		{
			updateParamValues();
			updateRobotInfo();
		}
		
		private function onEquipCalled(e:GameEvent):void//切换装备的机甲
		{
			//判断自己的等级是否能装备，设备耐久是否大于0;只能同时装备一个
			var robPart:RobotPart = _robotParts[_mainScrollBar.id];
			if(Global.user.level>=robPart.level && robPart.dur>0 && _equipRobotID!=_mainScrollBar.id)
			{
				_equipRobotID = _mainScrollBar.id;//标记当前装备机甲
				
				for each(var part:RobotPart in _robotParts)part.equip = false;
				_robotParts[_mainScrollBar.id].equip = true;
				
				refreshMainWeapon();
				refreshSecWeapon();
				
				updateRobotInfo();
			}
		}
		
		private function onInstallCalled(e:GameEvent):void//点击菜单中的安装后弹出选择接口菜单
		{
			var rob:RobotPart = _robotParts[_equipRobotID];
			
			switch(_mainScrollBar.type)
			{
				case 1://主武器
					_mainScrollBar.equipMenu.displayConnectors( rob.wep_m);
					break;
				case 2://副武器
					_mainScrollBar.equipMenu.displayConnectors( rob.wep_s);
					break;
				case 3://战场辅助物品
					
					break;
				default:
					break;
			}
		}
		
		private function onConnectCalled(e:GameEvent):void//选中安装接口后的事件
		{
			var rob:RobotPart = _robotParts[_equipRobotID];
			
			switch(_mainScrollBar.type)
			{
				case 1://主武器
					var mainWep:WeaponPartMain = _mainWeaponParts[_mainScrollBar.id];
					if(rob.level>=mainWep.level && mainWep.dur>0 && mainWep.installed == false)//满足条件才能安装
					{
						rob.wep_m[_mainScrollBar.connectorID] = mainWep.pid;
						
						refreshMainWeapon();
					}
					break;
				case 2://副武器
					var secWep:WeaponPartSec = _secWeaponParts[_mainScrollBar.id];
					if(rob.level>=secWep.level && secWep.dur>0 && secWep.installed == false)//满足条件才能安装
					{
						rob.wep_s[_mainScrollBar.connectorID] = secWep.pid;
						refreshSecWeapon();
					}
					break;
				case 3://战场辅助物品
					
					break;
				default:
					break;
			}
			updateRobotInfo();
		}
		
		private function onUninstallCalled(e:GameEvent):void//卸载已安装的武器
		{
			var rob:RobotPart = _robotParts[_equipRobotID];
			
			switch(_mainScrollBar.type)
			{
				case 1://主武器
					var mainWep:WeaponPartMain = _mainWeaponParts[_mainScrollBar.id];
					var index:int = rob.wep_m.indexOf(mainWep.pid);
					if(index!=-1)
					{
						rob.wep_m[index] = 0;
						refreshMainWeapon();
					}
					break;
				case 2://副武器
					var secWep:WeaponPartSec = _secWeaponParts[_mainScrollBar.id];
					index = rob.wep_s.indexOf(secWep.pid);
					if(index!=-1)
					{
						rob.wep_s[index] = 0;
						refreshSecWeapon();
					}
					break;
				case 3://战场辅助物品
					
					break;
				default:
					break;
			}
			
			updateRobotInfo();
		}
		private function onSellCalled(e:GameEvent):void//卖出会先弹出确认框
		{
			if(_mainScrollBar.type==0 && _mainScrollBar.id == _equipRobotID)return;
			
			addChild(_confirmBox);
			_confirmBox.confirmFun = confirmSell;
			_confirmBox.negateFun = cancel;
		}
		
		private function onIncreaseCalled(e:GameEvent):void//强化还没做
		{
			trace("increase:",_mainScrollBar.type,_mainScrollBar.id);
		}
		
		private function confirmSell():void//通知服务器卖掉装备，直接更新前端显示
		{
			Global.phpServer.call("shopServer.sell", new Responder(updated),Global.uid,_mainScrollBar.type,_mainScrollBar.id);
			
			switch(_mainScrollBar.type)
			{
				case 0:
					var rob:RobotPart = _robotParts[_mainScrollBar.id];
					_robotParts.splice(_mainScrollBar.id,1);
					_mainScrollBar.scrollBarList[_mainScrollBar.type].removeIcon(_mainScrollBar.id);
					removeChild(_confirmBox);
					_mainScrollBar.scrollBarList[_mainScrollBar.type].currentID = -1;
					var money:uint = Global.user.money+rob.sellCost;
					Tweener.addTween(Global.user,{money:money, time:0.5, transition:"linear" });
					break;
				case 1:
					var mainWep:WeaponPartMain = _mainWeaponParts[_mainScrollBar.id];
					_mainWeaponParts.splice(_mainScrollBar.id,1);
					_mainScrollBar.scrollBarList[_mainScrollBar.type].removeIcon(_mainScrollBar.id);
					removeChild(_confirmBox);
					_mainScrollBar.scrollBarList[_mainScrollBar.type].currentID = -1;
					money = Global.user.money+mainWep.sellCost;
					Tweener.addTween(Global.user,{money:money, time:0.5, transition:"linear" });
					for each(rob in _robotParts)
					{
						for(var i:int=0;i<rob.wep_m.length;i++)
						{
							if(rob.wep_m[i]==mainWep.pid)rob.wep_m[i] = 0;
						}
					}
					refreshMainWeapon();
					break;
				case 2:
					var secWep:WeaponPartSec = _secWeaponParts[_mainScrollBar.id];
					_secWeaponParts.splice(_mainScrollBar.id,1);
					_mainScrollBar.scrollBarList[_mainScrollBar.type].removeIcon(_mainScrollBar.id);
					removeChild(_confirmBox);
					_mainScrollBar.scrollBarList[_mainScrollBar.type].currentID = -1;
					money = Global.user.money+secWep.sellCost;
					Tweener.addTween(Global.user,{money:money, time:0.5, transition:"linear" });
					for each(rob in _robotParts)
					{
						for(i=0;i<rob.wep_s.length;i++)
						{
							if(rob.wep_s[i]==secWep.pid)rob.wep_s[i] = 0;
						}
					}
					refreshSecWeapon();
					break;
				case 3:
					
					break;
				
				default:
					break;
			}
		}
		
		private function updated(obj:Object):void
		{
			//trace("已经更新了:",obj);
		}
		private function fucked(obj:Object):void
		{
			trace("Server has be fucked");
		}
		
		private function cancel():void//在确认框中点“否”后
		{
			removeChild(_confirmBox);
		}
		//////////////////////////////////////////////////
		private function updateParamValues():void//更新参数面板上的参数值
		{
			for(var i:int=0;i<5;i++)
			{
				_mainScrollBar.paraRollout.paramValues[i].title = _paramNames[_mainScrollBar.type][i];
				_mainScrollBar.paraRollout.paramValues[i].type = _paramTypes[_mainScrollBar.type][i];
			}
			
			switch(_mainScrollBar.type)
			{
				case 0:
					var rob:RobotPart = _robotParts[_mainScrollBar.id];
					_mainScrollBar.paraRollout.paramValues[0].value = rob.level;
					_mainScrollBar.paraRollout.paramValues[1].value = rob.def;
					_mainScrollBar.paraRollout.paramValues[2].value = rob.speed;
					_mainScrollBar.paraRollout.paramValues[3].value = rob.hp;
					_mainScrollBar.paraRollout.paramValues[4].value = rob.dur;
					_mainScrollBar.paraRollout.paramValues[3].maxValue = rob.maxhp;
					_mainScrollBar.paraRollout.paramValues[4].maxValue = rob.maxdur;
					break;
				case 1:
					var mainWep:WeaponPartMain = _mainWeaponParts[_mainScrollBar.id];
					_mainScrollBar.paraRollout.paramValues[0].value = mainWep.level;
					_mainScrollBar.paraRollout.paramValues[1].value = mainWep.maxBullets;
					_mainScrollBar.paraRollout.paramValues[2].value = mainWep.speed;
					_mainScrollBar.paraRollout.paramValues[3].value = mainWep.pow;
					_mainScrollBar.paraRollout.paramValues[4].value = mainWep.dur;
					_mainScrollBar.paraRollout.paramValues[4].maxValue = mainWep.maxdur;
					break;
				case 2:
					var secWep:WeaponPartSec = _secWeaponParts[_mainScrollBar.id];
					_mainScrollBar.paraRollout.paramValues[0].value = secWep.level;
					_mainScrollBar.paraRollout.paramValues[1].value = secWep.speed;
					_mainScrollBar.paraRollout.paramValues[2].value = secWep.pow;
					_mainScrollBar.paraRollout.paramValues[3].value = secWep.dur;
					_mainScrollBar.paraRollout.paramValues[3].maxValue = secWep.maxdur;
					_mainScrollBar.paraRollout.paramValues[4].string = "";
					break;
				case 3:
					break;
				default:
					break;
			}
		}
		
		private function updateRobotInfo():void
		{
			var robinfo:RobotInfo = _mainScrollBar.robotInfo;
			var thisRob:RobotPart = _robotParts[_equipRobotID];
			
			robinfo.hp.value = thisRob.hp;
			robinfo.def.value = thisRob.def;
			robinfo.speed.value = thisRob.speed;
			
			var act:int=0;
			for each(var mainWep:WeaponPartMain in _mainWeaponParts)
				if(thisRob.wep_m.indexOf(mainWep.pid)!=-1)act+=mainWep.pow*mainWep.speed;
			for each(var secWep:WeaponPartSec in _secWeaponParts)
				if(thisRob.wep_s.indexOf(secWep.pid)!=-1)act+=secWep.pow*secWep.speed;
			
			robinfo.act.value = act;
			
			switch(_mainScrollBar.type)
			{
				case 0:
					var nextRob:RobotPart = _robotParts[_mainScrollBar.id];
					robinfo.hp.nextValue = nextRob.hp;
					robinfo.def.nextValue = nextRob.def;
					robinfo.speed.nextValue = nextRob.speed;
					
					act=0;
					for each(mainWep in _mainWeaponParts)
						if(nextRob.wep_m.indexOf(mainWep.pid)!=-1)act+=mainWep.pow*mainWep.speed;
					for each(secWep in _secWeaponParts)
						if(nextRob.wep_s.indexOf(secWep.pid)!=-1)act+=secWep.pow*secWep.speed;
					robinfo.act.nextValue = act;
					
					break;
				case 1:
					var badWep:int = -1;
					for(var i:int=0;i<_mainWeaponParts.length;i++)
					{
						mainWep = _mainWeaponParts[i];
						if(thisRob.wep_m.indexOf(mainWep.pid)!=-1)
						{
							if(badWep==-1)badWep = i;
							if(mainWep.pow*mainWep.speed < _mainWeaponParts[badWep].pow*_mainWeaponParts[badWep].speed)badWep=i;
						}
					}
					
					var hasConnector:Boolean = false;
					for each(var pid:int in thisRob.wep_m)
					{
						if(pid==0)
						{
							hasConnector = true;
							break;
						}
					}
					
					mainWep = _mainWeaponParts[_mainScrollBar.id];
					if(mainWep.icon.installed)
					{
						robinfo.act.nextValue = robinfo.act.value;
					}else
					{
						if(hasConnector)
						{
							robinfo.act.nextValue = robinfo.act.value+mainWep.pow*mainWep.speed;
						}else
						{
							robinfo.act.nextValue = robinfo.act.value-_mainWeaponParts[badWep].pow*_mainWeaponParts[badWep].speed+mainWep.pow*mainWep.speed;
						}
					}
					
					robinfo.hp.nextValue = robinfo.hp.value
					robinfo.def.nextValue = robinfo.def.value
					robinfo.speed.nextValue = robinfo.speed.value
					
					break;
				case 2:
					badWep = -1;
					for(i=0;i<_secWeaponParts.length;i++)
					{
						secWep = _secWeaponParts[i];
						if(thisRob.wep_s.indexOf(secWep.pid)!=-1)
						{
							if(badWep==-1)badWep = i;
							if(secWep.pow*secWep.speed < _secWeaponParts[badWep].pow*_secWeaponParts[badWep].speed)badWep=i;
						}
					}
					
					hasConnector = false;
					for each(pid in thisRob.wep_s)
					{
						if(pid==0)
						{
							hasConnector = true;
							break;
						}
					}
					
					secWep = _secWeaponParts[_mainScrollBar.id];
					if(secWep.icon.installed)
					{
						robinfo.act.nextValue = robinfo.act.value;
					}else
					{
						if(hasConnector)
						{
							robinfo.act.nextValue = robinfo.act.value+secWep.pow*secWep.speed;
						}else
						{
							robinfo.act.nextValue = robinfo.act.value-_secWeaponParts[badWep].pow*_secWeaponParts[badWep].speed+secWep.pow*secWep.speed;
						}
					}
					
					robinfo.hp.nextValue = robinfo.hp.value
					robinfo.def.nextValue = robinfo.def.value
					robinfo.speed.nextValue = robinfo.speed.value
					
					break;
				case 3:
					
					break;
					
				default:
					break;
			}
			
			
			
		}
		
		private function refreshMainWeapon():void//更新主武器
		{
			var rob:RobotPart = _robotParts[_equipRobotID];
			for each(var mainWep:WeaponPartMain in _mainWeaponParts)
			{
				if(rob.wep_m.indexOf(mainWep.pid)==-1)
				{
					mainWep.installed = false;
				}else mainWep.installed = true;
			}
			
			refreshModel();
		}
		
		private function refreshSecWeapon():void//更新副武器
		{
			var rob:RobotPart = _robotParts[_equipRobotID];
			
			for each(var secWep:WeaponPartSec in _secWeaponParts)
			{
				if(rob.wep_s.indexOf(secWep.pid)==-1)
				{
					secWep.installed = false;
				}else secWep.installed = true;
			}
			
			refreshModel();
		}
		
		private function refreshModel():void//更新场景中的3D模型
		{
			for each(var rob:RobotPart in _robotParts)
			{
				rob.hideModel();
				try//还没加载完的模型不能做这些事
				{
					for(var i:int=0;i<rob.wep_m.length;i++)
					{
						var robRes:RobotResource = rob.resource as RobotResource;
						var connector:WeaponConnector = robRes.getWeaponConnector(1,i);
						connector.remove();
					}
					
					for(i=0;i<rob.wep_s.length;i++)
					{
						connector = robRes.getWeaponConnector(2,i);
						connector.remove();
					}
				} 
				catch(e:Error) {}
			}
			
			rob = _robotParts[_equipRobotID];
			rob.displayModel();
			
			if(rob.resource)refeshWeaponModel(null);
			else rob.addEventListener(Event.COMPLETE,refeshWeaponModel);
		}
		
		private function refeshWeaponModel(e:Event):void//更新场景中的3D武器模型
		{
			var rob:RobotPart = _robotParts[_equipRobotID];
			var robRes:RobotResource = rob.resource as RobotResource;
			for(var i:int=0;i<rob.wep_m.length;i++)
			{
				var connector:WeaponConnector = robRes.getWeaponConnector(1,i);
				connector.remove();
				
				var pid:int = rob.wep_m[i];
				if(pid != 0)
				{
					for each(var mainWep:WeaponPartMain in _mainWeaponParts)
					{
						if(mainWep.pid == pid)
						{
							var wepRes:WeaponResource = mainWep.resource as WeaponResource;
							connector.install(wepRes);
							break;
						}
					}
				}
			}
			
			for(i=0;i<rob.wep_s.length;i++)
			{
				connector = robRes.getWeaponConnector(2,i);
				connector.remove();
				
				pid = rob.wep_s[i];
				if(pid != 0)
				{
					for each(var secWep:WeaponPartSec in _secWeaponParts)
					{
						if(secWep.pid == pid)
						{
							wepRes = secWep.resource as WeaponResource;
							connector.install(wepRes);
							break;
						}
					}
				}
			}
		}
		
		private function cameraLoaded(e:Event):void//收到相机信息并处理
		{
			var settingLoader:URLLoader = e.currentTarget as URLLoader;
			settingLoader.removeEventListener(Event.COMPLETE,cameraLoaded);
			
			var xml:XML = new XML(settingLoader.data);
			Global.gameView.getCamera(xml);
			
			settingLoader.addEventListener(Event.COMPLETE,LightsLoaded);
			settingLoader.load(new URLRequest(_lightsSetting));
		}
		
		private function LightsLoaded(e:Event):void//收到灯光信息并处理
		{
			var settingLoader:URLLoader = e.currentTarget as URLLoader;
			settingLoader.removeEventListener(Event.COMPLETE,LightsLoaded);
			
			var xml:XML = new XML(settingLoader.data);
			Global.gameView.getLight(xml);
		}
		
		private function onResize(e:Event):void
		{
			_quitButton.x = _mainScrollBar.width-_quitButton.width;
			_quitButton.y = -_quitButton.height-9;
			_title.x = Global.stage.stageWidth-_title.bitmapData.width;
		}
		
		private function onRemoved(e:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE,onRemoved);
			Global.stage.removeEventListener(Event.RESIZE,onResize);
			_mainScrollBar.removeEventListener(GameEvent.ITEM_CALL_SELECT,onSelectCalled);
			_mainScrollBar.removeEventListener(GameEvent.ITEM_CALL_EQUIP,onEquipCalled);
			_mainScrollBar.removeEventListener(GameEvent.ITEM_CALL_INSTALL,onInstallCalled);
			_mainScrollBar.removeEventListener(GameEvent.ITEM_CALL_UNINSTALL,onUninstallCalled);
			_mainScrollBar.removeEventListener(GameEvent.ITEM_CALL_CONNECT,onConnectCalled);
			_mainScrollBar.removeEventListener(GameEvent.ITEM_CALL_INCREASE,onIncreaseCalled);
			_mainScrollBar.removeEventListener(GameEvent.ITEM_CALL_SELL,onSellCalled);
			
		}
		
	}
}